/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.ieproject.presentation;

import java.awt.Color;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import uit.ieproject.business.AddressBLO;
import uit.ieproject.entities.Address;
import uit.ieproject.utilites.StripedTableCellRenderer;
import uit.ieproject.utilites.TableUtil;

/**
 *
 * @author Maivia
 */
public class AddressFrame extends javax.swing.JFrame {

    private AddressBLO addressBLO = new AddressBLO();
    private Long idSelected = 0L;
    private final String ADDRESS_PATTERN = "(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?";
    private boolean isExistAddress;
    private boolean isAddress;
    private boolean isTitleRegex;
    private boolean isContentRegex;
    private boolean isImageRegex;

    /**
     * Creates new form AddressFrame
     */
    public AddressFrame() {
        try {
            initComponents();
            //Set icon image:
            ImageIcon icon = new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/manage.png"));
            this.setIconImage(icon.getImage());
            this.setLocationRelativeTo(null);
            TableUtil.formatTable(tbListAddress);
            StripedTableCellRenderer.installInTable(tbListAddress, Color.getHSBColor(1, 100, 217), null, null, null);
            ArrayList<Address> addresses = addressBLO.getAllAddress();
            showList(addresses, tbListAddress);
            reset();
        } catch (SQLException ex) {
            Logger.getLogger(AddressFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void reset() {
        btAddAddress.setEnabled(false);
        btEditAddress.setEnabled(false);
        btDeleteAddress.setEnabled(false);
        tfAddress.setText("");
        tfContentRegex.setText("");
        tfTitleRegex.setText("");
        tfImageRegex.setText("");
        idSelected = 0L;
    }

    private void initValue() {
        isExistAddress = existedAddress();
        isAddress = isMatchedPattern(ADDRESS_PATTERN, tfAddress.getText().toString());
        isTitleRegex = isValidRegex(tfTitleRegex.getText().toString());
        isContentRegex = isValidRegex(tfContentRegex.getText().toString());
        isImageRegex = isValidImageRegex(tfImageRegex.getText().toString());
    }

    private void showList(ArrayList<Address> listAddress, JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setNumRows(0);
        int length = listAddress.size();
        for (int i = 0; i < length; i++) {
            Address address = listAddress.get(i);
            model.addRow(new Object[]{i + 1, address.getAddress(), address.getTitle(), address.getParagraph(), address.getImage(), address.getId()});
        }
    }

    private long[] getIdsSelected(JTable table) {
        long[] result;
        int[] selected = table.getSelectedRows();
        int numSelectedRegexs = selected.length;
        result = new long[numSelectedRegexs];
        if (numSelectedRegexs > 0) {
            for (int i = 0; i < numSelectedRegexs; i++) {
                result[i] = Long.valueOf(table.getValueAt(selected[i], 5).toString());
            }
        }
        return result;
    }

    private boolean existedAddress() {
        boolean isExist = false;
        try {
            String strAddress = tfAddress.getText().trim();
            ArrayList<String> strAddresses = addressBLO.getAddressColumn();
            int length = strAddresses.size();
            for (int i = 0; i < length; i++) {
                if (strAddress.equals(strAddresses.get(i))) {
                    isExist = true;
                    break;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(AddressFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isExist;
    }

    private boolean isMatchedPattern(String regex, String inputStr) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputStr);
        return matcher.matches();
    }

    private boolean isValidRegex(String regex) {
        try {
            Pattern.compile(regex);
            if (regex.isEmpty()) {
                return false;
            } else {
                return true;
            }
        } catch (PatternSyntaxException ex) {
            return false;
        }
    }

    private boolean isValidImageRegex(String regex) {
        try {
            Pattern.compile(regex);
            return true;

        } catch (PatternSyntaxException ex) {
            return false;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        tfAddress = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfTitleRegex = new javax.swing.JTextField();
        tfContentRegex = new javax.swing.JTextField();
        tfImageRegex = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        btAddAddress = new javax.swing.JButton();
        btEditAddress = new javax.swing.JButton();
        btDeleteAddress = new javax.swing.JButton();
        btReset = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbListAddress = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản lý địa chỉ");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Địa chỉ");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        tfAddress.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfAddressKeyReleased(evt);
            }
        });

        jLabel2.setText("Biểu thức chính quy");

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jLabel3.setText("Tiêu đề");

        jLabel4.setText("Nội dung");

        jLabel5.setText("Hình ảnh");

        tfTitleRegex.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfTitleRegexKeyReleased(evt);
            }
        });

        tfContentRegex.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfContentRegexKeyReleased(evt);
            }
        });

        tfImageRegex.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfImageRegexKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfTitleRegex)
                            .addComponent(tfContentRegex)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfImageRegex)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfTitleRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfContentRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tfImageRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        btAddAddress.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/add.png"))); // NOI18N
        btAddAddress.setText("Thêm mới");
        btAddAddress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddAddressActionPerformed(evt);
            }
        });

        btEditAddress.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/edit (2).png"))); // NOI18N
        btEditAddress.setText("Thay đổi");
        btEditAddress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditAddressActionPerformed(evt);
            }
        });

        btDeleteAddress.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/Remove24.png"))); // NOI18N
        btDeleteAddress.setText("Hủy bỏ");
        btDeleteAddress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteAddressActionPerformed(evt);
            }
        });

        btReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/refresh.png"))); // NOI18N
        btReset.setText("Làm mới");
        btReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(btReset)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btAddAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btEditAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btDeleteAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btAddAddress, btReset});

        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btReset)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btAddAddress)
                        .addComponent(btEditAddress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btDeleteAddress)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btAddAddress, btReset});

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfAddress)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tfAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel6.setText("Danh sách địa chỉ");

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        tbListAddress.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Địa chỉ", "Tiêu đề", "Nội dung", "Hình ảnh", "Mã"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbListAddress.setGridColor(new java.awt.Color(153, 204, 255));
        tbListAddress.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbListAddressMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbListAddressMousePressed(evt);
            }
        });
        jScrollPane3.setViewportView(tbListAddress);
        tbListAddress.getColumnModel().getColumn(0).setMinWidth(30);
        tbListAddress.getColumnModel().getColumn(0).setPreferredWidth(30);
        tbListAddress.getColumnModel().getColumn(0).setMaxWidth(30);
        tbListAddress.getColumnModel().getColumn(5).setMinWidth(0);
        tbListAddress.getColumnModel().getColumn(5).setPreferredWidth(0);
        tbListAddress.getColumnModel().getColumn(5).setMaxWidth(0);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAddAddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddAddressActionPerformed
        try {
            // TODO add your handling code here:
            String strAddress = tfAddress.getText().trim();
            String strTitle = tfTitleRegex.getText().trim();
            String strParagraph = tfContentRegex.getText().trim();
            String strImage = tfImageRegex.getText().trim();
            Address address = new Address();
            address.setAddress(strAddress);
            address.setTitle(strTitle);
            address.setParagraph(strParagraph);
            address.setImage(strImage);
            addressBLO.addAddress(address);
            ArrayList<Address> listAddresses = addressBLO.getAllAddress();
            if (listAddresses.size() > 0) {
                showList(listAddresses, tbListAddress);
            }
            reset();
        } catch (SQLException ex) {
            Logger.getLogger(AddressFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btAddAddressActionPerformed

    private void btEditAddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditAddressActionPerformed
        try {
            // TODO add your handling code here:
            //Get information of clicked item:
            Address addressSelected = new Address();
            addressSelected.setId(idSelected);
            addressSelected.setAddress(tfAddress.getText().trim());
            addressSelected.setTitle(tfTitleRegex.getText().trim());
            addressSelected.setParagraph(tfContentRegex.getText().trim());
            addressSelected.setImage(tfImageRegex.getText().trim());
            addressBLO.editAddress(addressSelected);
            ArrayList<Address> listAddresses = addressBLO.getAllAddress();
            if (listAddresses.size() > 0) {
                showList(listAddresses, tbListAddress);
            }
            tfAddress.setEnabled(true);
        } catch (SQLException ex) {
            Logger.getLogger(AddressFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btEditAddressActionPerformed

    private void btDeleteAddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteAddressActionPerformed
        // TODO add your handling code here:
        try {
            int dialogResult = JOptionPane.showConfirmDialog(null, "Bạn có chắc chắn muốn xóa không?", "Warning", JOptionPane.YES_NO_OPTION);
            if (dialogResult == JOptionPane.YES_OPTION) {
                long[] idsSelected = getIdsSelected(tbListAddress);
                addressBLO.deleteAddress(idsSelected);
                ArrayList<Address> addresses = addressBLO.getAllAddress();
                showList(addresses, tbListAddress);
            }
            reset();
        } catch (SQLException ex) {
        } finally {
        }

    }//GEN-LAST:event_btDeleteAddressActionPerformed

    private void tbListAddressMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbListAddressMouseClicked
        if (evt.getClickCount() == 1) {
        } else {
            int row = tbListAddress.getSelectedRow();
            btAddAddress.setEnabled(false);
            btEditAddress.setEnabled(true);
            btDeleteAddress.setEnabled(true);
            tfAddress.setBackground(Color.white);
            tfTitleRegex.setBackground(Color.white);
            tfContentRegex.setBackground(Color.white);
            tfImageRegex.setBackground(Color.white);
            if (row < 0) {
                return;
            }
            idSelected = Long.valueOf(tbListAddress.getValueAt(row, 5).toString());
            String address = tbListAddress.getValueAt(row, 1).toString();
            String title = tbListAddress.getValueAt(row, 2).toString();
            String paragraph = tbListAddress.getValueAt(row, 3).toString();
            String image = tbListAddress.getValueAt(row, 4).toString();
            tfAddress.setText(address);
            tfTitleRegex.setText(title);
            tfContentRegex.setText(paragraph);
            tfImageRegex.setText(image);
        }
    }//GEN-LAST:event_tbListAddressMouseClicked

    private void tfAddressKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfAddressKeyReleased
        // TODO add your handling code here:
        boolean isAdd = true;
        initValue();
        if (isExistAddress) {
            isAdd = false;
        }
        if (isAddress) {
            tfAddress.setForeground(Color.blue);
        }
        if (!isAddress) {
            //reset();
            btAddAddress.setEnabled(false);
            btEditAddress.setEnabled(false);
            btDeleteAddress.setEnabled(false);
            tfAddress.setForeground(Color.red);
            //JOptionPane.showConfirmDialog(null, "Địa chỉ không đúng", "Thông báo", JOptionPane.CANCEL_OPTION);
        } else if (isExistAddress) {
            btAddAddress.setEnabled(false);
            tfAddress.setText("");
            JOptionPane.showConfirmDialog(null, "Địa chỉ đã tồn tại", "Thông báo", JOptionPane.CANCEL_OPTION);
        } else if (isAdd && isAddress && isTitleRegex && isContentRegex && isImageRegex) {
            btAddAddress.setEnabled(true);
        } else {
            btAddAddress.setEnabled(false);
        }
        btEditAddress.setEnabled(false);
        btDeleteAddress.setEnabled(false);
    }//GEN-LAST:event_tfAddressKeyReleased

    private void tfTitleRegexKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfTitleRegexKeyReleased
        // TODO add your handling code here:
        initValue();
        btDeleteAddress.setEnabled(false);
        if (tbListAddress.getSelectedRow() < 0 || !isExistAddress) {
            btEditAddress.setEnabled(false);
        } else if (isAddress && isTitleRegex && isContentRegex && isImageRegex && isExistAddress) {
            btEditAddress.setEnabled(true);
        }
        if (isTitleRegex) {
            tfTitleRegex.setForeground(Color.blue);
        }
        if (!isTitleRegex) {
            btEditAddress.setEnabled(false);
            btAddAddress.setEnabled(false);
            tfTitleRegex.setForeground(Color.red);
            //JOptionPane.showConfirmDialog(null, "Biểu thức chính quy tiêu đề chưa đúng", "Thông báo", JOptionPane.CANCEL_OPTION);
        } else if (!isExistAddress && isAddress && isTitleRegex && isContentRegex && isImageRegex) {
            btAddAddress.setEnabled(true);
        } else {
            btAddAddress.setEnabled(false);
        }
    }//GEN-LAST:event_tfTitleRegexKeyReleased

    private void tfContentRegexKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfContentRegexKeyReleased
        // TODO add your handling code here:
        initValue();
        btDeleteAddress.setEnabled(false);
        if (tbListAddress.getSelectedRow() < 0 || !isExistAddress) {
            btEditAddress.setEnabled(false);
        } else if (isAddress && isTitleRegex && isContentRegex && isImageRegex && isExistAddress) {
            btEditAddress.setEnabled(true);
        }
        if (isContentRegex) {
            tfContentRegex.setForeground(Color.blue);
        }
        if (!isContentRegex) {
            btEditAddress.setEnabled(false);
            btAddAddress.setEnabled(false);
            tfContentRegex.setForeground(Color.red);
            //JOptionPane.showConfirmDialog(null, "Biểu thức nội dung chưa đúng", "Thông báo", JOptionPane.CANCEL_OPTION);
        } else if (!isExistAddress && isAddress && isTitleRegex && isContentRegex && isImageRegex) {
            btAddAddress.setEnabled(true);
        } else {
            btAddAddress.setEnabled(false);
        }
    }//GEN-LAST:event_tfContentRegexKeyReleased

    private void tfImageRegexKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfImageRegexKeyReleased
        // TODO add your handling code here:
        btDeleteAddress.setEnabled(false);
        initValue();
        if (tbListAddress.getSelectedRow() < 0 || !isExistAddress) {
            btEditAddress.setEnabled(false);
        } else if (isAddress && isTitleRegex && isContentRegex && isImageRegex && isExistAddress) {
            btEditAddress.setEnabled(true);
        }
        if (isImageRegex) {
            tfImageRegex.setForeground(Color.blue);
        }
        if (!isImageRegex) {
            btAddAddress.setEnabled(false);
            btEditAddress.setEnabled(false);
            tfImageRegex.setForeground(Color.red);
            //JOptionPane.showConfirmDialog(null, "Biểu thức hình ảnh chưa đúng", "Thông báo", JOptionPane.CANCEL_OPTION);
        } else if (!isExistAddress && isAddress && isTitleRegex && isContentRegex && isImageRegex) {
            btAddAddress.setEnabled(true);
        } else {
            btAddAddress.setEnabled(false);
        }
    }//GEN-LAST:event_tfImageRegexKeyReleased

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        MainFrame.showCbAddress(MainFrame.combobox);
    }//GEN-LAST:event_formWindowClosed

    private void tbListAddressMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbListAddressMousePressed
        JTable table = (JTable) evt.getSource();
        if (evt.getClickCount() == 1) {
            int[] selectedRows = table.getSelectedRows();
            if (selectedRows.length >= 1) {
                btDeleteAddress.setEnabled(true);
            }
        } else {
            int row = table.getSelectedRow();
            if (row < 0) {
                return;
            }
            tfAddress.setForeground(Color.blue);
            tfTitleRegex.setForeground(Color.blue);
            tfContentRegex.setForeground(Color.blue);
            tfImageRegex.setForeground(Color.blue);
            idSelected = Long.valueOf(tbListAddress.getValueAt(row, 5).toString());
            String address = tbListAddress.getValueAt(row, 1).toString();
            String title = tbListAddress.getValueAt(row, 2).toString();
            String paragraph = tbListAddress.getValueAt(row, 3).toString();
            String image = tbListAddress.getValueAt(row, 4).toString();
            tfAddress.setText(address);
            tfTitleRegex.setText(title);
            tfContentRegex.setText(paragraph);
            tfImageRegex.setText(image);
            btAddAddress.setEnabled(false);
        }
    }//GEN-LAST:event_tbListAddressMousePressed

    private void btResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btResetActionPerformed
        reset();
    }//GEN-LAST:event_btResetActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddAddress;
    private javax.swing.JButton btDeleteAddress;
    private javax.swing.JButton btEditAddress;
    private javax.swing.JButton btReset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tbListAddress;
    private javax.swing.JTextField tfAddress;
    private javax.swing.JTextField tfContentRegex;
    private javax.swing.JTextField tfImageRegex;
    private javax.swing.JTextField tfTitleRegex;
    // End of variables declaration//GEN-END:variables
}
